package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.operation.domain.bo.TOperationdailyBo;
import com.ruoyi.operation.domain.vo.TOperationdailyVo;
import com.ruoyi.operation.domain.TOperationdaily;
import com.ruoyi.operation.mapper.TOperationdailyMapper;
import com.ruoyi.operation.service.ITOperationdailyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 运营部门日志记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-10
 */
@RequiredArgsConstructor
@Service
public class TOperationdailyServiceImpl implements ITOperationdailyService {

    private final TOperationdailyMapper baseMapper;

    /**
     * 查询运营部门日志记录
     */
    @Override
    public TOperationdailyVo queryById(String id){
        return baseMapper.selectVoDetailById(id);
    }

    /**
     * 查询运营部门日志记录列表
     */
    @Override
    public List<TOperationdailyVo> queryPageList(TOperationdailyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TOperationdaily> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(TOperationdaily::getSort);
        List<TOperationdailyVo> result = baseMapper.selectVoList(lqw);
        return result;
    }

    /**
     * 查询运营部门日志记录列表
     */
    @Override
    public List<TOperationdailyVo> queryList(TOperationdailyBo bo) {
        LambdaQueryWrapper<TOperationdaily> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TOperationdaily> buildQueryWrapper(TOperationdailyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TOperationdaily> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTemplatetype()), TOperationdaily::getTemplatetype, bo.getTemplatetype());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), TOperationdaily::getSummary, bo.getSummary());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatetime()), TOperationdaily::getOperatetime, bo.getOperatetime());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TOperationdaily::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), TOperationdaily::getPeriod, bo.getPeriod());
        lqw.eq(bo.getSpecificdate() != null, TOperationdaily::getSpecificdate, bo.getSpecificdate());
        lqw.eq(bo.getSort() != null, TOperationdaily::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), TOperationdaily::getFlag, bo.getFlag());
//        lqw.eq(StringUtils.isNotBlank(bo.getBusioperations()), TOperationdaily::getBusioperations, bo.getBusioperations());
        return lqw;
    }

    /**
     * 新增运营部门日志记录
     */
    @Override
    public Boolean insertByBo(TOperationdailyBo bo) {
        TOperationdaily add = BeanUtil.toBean(bo, TOperationdaily.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改运营部门日志记录
     */
    @Override
    public Boolean updateByBo(TOperationdailyBo bo) {
        TOperationdaily update = BeanUtil.toBean(bo, TOperationdaily.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TOperationdaily entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除运营部门日志记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TOperationdailyVo> getTodaysituation(TOperationdailyBo bo) {
        return baseMapper.getTodaysituation(bo);
    }

}
